'use strict';

/**
 * @name 全局的消息提醒模块
 * @description 当有某些事件发生时，需要提醒用户，可以调用改服务的对应方法
 * @author Yud
 */
angular.module('metalabPartnerApp').factory('MetaRemind', ['toaster', function (toaster) {
    var reminds = {};

    reminds.info = function (title, body) {
        return toaster.pop({
            type: 'info',
            title: title,
            body: body
        });
    };

    reminds.success = function (title, body) {
        return toaster.pop({
            type: 'success',
            title: title,
            body: body
        });
    };

    reminds.error = function (title, body) {
        if (title instanceof Object) {
            var header = title.data.message;
        } else {
            var header = title;
        }
        return toaster.pop({
            type: 'error',
            title: header,
            body: body,
            timeout: 10000
        });
    };

    reminds.warn = function (title, body) {
        return toaster.pop({
            type: 'warning',
            title: title,
            body: body,
            timeout: 8000
        });
    };

    reminds.clear = function (id) {
        if (id) {
            toaster.clear(id);
        } else {
            toaster.clear();
        }
    };

    reminds.serverError = function (error) {
        var body = error.data.message;
        if (body === undefined) {
            switch (error.status) {
                case 404:
                    body = '未找到该页面，请检查url地址';
                    break;
                case 500:
                    body = '服务器发生错误，请稍后刷新重试。如果情况较为紧急，可联系客服热线021-54336693';
                    break;
                default:
                    body = '发生了些未知错误，请稍后刷新重试。如果情况较为紧急，可联系客服热线021-54336693';
                    break;
            }
        }
        return toaster.pop({
            type: 'error',
            title: '出错啦',
            body: body,
            timeout: 10000
        });
    };

    return reminds;
}]);
